package com.uniplaza.app.base;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.android.volley.RequestQueue;
import com.android.volley.components.volley.EventCallback;
import com.umeng.analytics.MobclickAgent;
import com.uniplaza.app.utils.Utils;
import com.uniplaza.app.volley.ErrorToolKit;


/**
 * 所有 activity 的父类  进行必要的验证
 * Created by Kim on 16/6/6.
 */
public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener, EventCallback {
    private String TAG = BaseActivity.class.getSimpleName();
    protected static RequestQueue mQueue;
    protected Activity mActivity;
    protected static boolean isLogin;
    protected boolean isFragment = false;
//    public boolean LoadingShut = true;

    protected abstract void initObjects();

    protected abstract void initViews();

    protected abstract void initListeners();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = this;
        mQueue = Application.getInstance().getmQueue();
        // 个推 初始化
//        PushManager.getInstance().initialize(mActivity, GetuiPushService.class);
//        PushManager.getInstance().registerPushIntentService(mActivity, GetuiIntentService.class);

    }

    /**
     * 初始化所有Activity数据并显示在屏幕上
     *
     * @param layoutResID layout资源文件
     */
    protected void createAll(int layoutResID) {
        setContentView(layoutResID);

        Utils.logError(TAG, " createAll --> " + getClass().getName());
        initActivity();
    }

    /**
     * 初始化所有相关的数据
     */
    private void initActivity() {
//        Utils.log(TAG, "initViews");
        initObjects();
        initViews();
        initListeners();

    }


    /**
     * 获取权限  但是必须重写permissionResutl 方法获取返回值
     */
    public boolean GetPermission(String[] permission, int CallBackNum) {

        int p = 0;
        boolean istrue;
        istrue = true;

        for (int i = 0; i < permission.length; i++) {
            p = ContextCompat.checkSelfPermission(this, permission[i]);
            Utils.log(TAG, "权限 " + i + " ---- " + p);
            if (p != PackageManager.PERMISSION_GRANTED) {//判断是没有此权限
                Utils.log(TAG, "是这个没有权限");
                istrue = false;
            }
        }

        Utils.log(TAG, "权限 ----- " + istrue);

        if (!istrue) {
            ActivityCompat.requestPermissions(this, permission, CallBackNum);
            return false;
        } else {
            PermissionResutl(true, CallBackNum);
            return true;
        }

    }

    /**
     * 获取权限 请重写permissionResutl 方法
     */
    protected void PermissionResutl(boolean result, int CallBackNum) {
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            this.PermissionResutl(true, requestCode);
        } else {
            this.PermissionResutl(false, requestCode);
        }
    }


    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!isFragment){   // 当不包含 fragment 时进行 统计页面，否则在fragment中统计
            MobclickAgent.onPageStart(getClass().getName());
        }
        MobclickAgent.onResume(this);


    }

    @Override
    protected void onPause() {
        super.onPause();
        // 友盟统计
        if (!isFragment){   // 当不包含 fragment 时进行 统计页面，否则在fragment中统计
            MobclickAgent.onPageEnd(getClass().getName());
        }
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mQueue != null) {
            mQueue.cancelAll(this);
        }
    }

    @Override
    public void errorNetWork(String nRequestType, int error, String message) {
        ErrorToolKit.tostError(mActivity, error, message);
    }

    @Override
    public void finish() {
        super.finish();
        Utils.logError(TAG, " finish --> " + getClass().getName());
        View layoutView = getCurrentFocus();
        if (layoutView != null) {
            Utils.hideKeyboard(mActivity, layoutView);
        }
    }
}
